package org.xiaojl.modules.drp.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xiaojl.common.base.BaseController;
import org.xiaojl.dwz.DwzAjax;
import org.xiaojl.dwz.PageBean;
import org.xiaojl.dwz.PageParam;
import org.xiaojl.modules.drp.entity.DrpProductType;
import org.xiaojl.modules.drp.entity.DrpSupplier;
import org.xiaojl.modules.drp.service.DrpSupplierService;
import org.xiaojl.utils.ToolUtil;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * <p>
 * 供货商 前端控制器
 * </p>
 *
 * @author xiaojl
 * @since 2017-09-06
 */
@Controller
@RequestMapping("/drp/drpSupplier")
public class DrpSupplierController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(DrpSupplierController.class);
	private static final String basePagePath = "drp/supplier/";
	@Autowired
	private DrpSupplierService supplierService;
	
	/**
	 * 查询数据，进入列表页面
	 */
	@RequiresPermissions("drp/drpSupplier/list")
	@RequestMapping("/list")
	public ModelAndView list(PageParam pageParam, DrpSupplier entity, Model model){
		try {
			PageBean<DrpSupplier> pageBean = this.selectListPage(pageParam, entity, supplierService);
			
			model.addAttribute("pageBean", pageBean);
			model.addAttribute("pageParam", pageParam);
			model.addAttribute("entity", entity);
			
			return operatePage(basePagePath+"list",model);
		} catch (Exception e) {
			log.error("查询列表数据出现异常:", e);
			return operateError("获取数据失败");
		}
	}
	
	@RequestMapping("/selectList")
	@ResponseBody
	public Object selectList(){
		List<DrpSupplier> list = supplierService.selectList(new EntityWrapper<DrpSupplier>().orderBy("createTime",true));
		return list;
	}
	
	/**
	 * 转到添加页面 .
	 */
	@RequiresPermissions("drp/drpSupplier/add")
	@RequestMapping("/addUI")
	public ModelAndView addUI(Model model) {
		try {
			return operatePage(basePagePath+"add",model);
			
		} catch (Exception e) {
			log.error("进入添加页面出现异常:", e);
			return operateError("获取数据失败");
		}
	}
	
	/**
	 * 保存 .
	 */
	@RequiresPermissions("drp/drpSupplier/add")
	@RequestMapping("/add")
	public ModelAndView add(Model model, @Valid DrpSupplier entity, BindingResult result, DwzAjax dwz) {
		try {
			if(result.hasErrors()){
				return operateError(result.getAllErrors().get(0).getDefaultMessage());
			}
			
			List<DrpSupplier> listByBeanId = supplierService.selectList(new EntityWrapper<DrpSupplier>().eq("name",entity.getName()));
			if (ToolUtil.isNotEmpty(listByBeanId)) {
				return operateError("供货商名称【" + entity.getName() + "】已存在");
			}

			supplierService.insert(entity);
			
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("添加数据出现异常:", e);
			return operateError("保存数据失败");
		}
	}
	
	/**
	 * 转到修改页面 .
	 */
	@RequiresPermissions("drp/drpSupplier/edit")
	@RequestMapping("/editUI")
	public ModelAndView editUI(Model model, Long id) {
		try {
			DrpSupplier entity = supplierService.selectById(id);
			if (ToolUtil.isEmpty(entity)) {
				return operateError("获取数据失败");
			}

			model.addAttribute("entity",entity);
			return operatePage(basePagePath+"edit",model);
		} catch (Exception e) {
			log.error("进入编辑页面时，获取数据出现异常:id="+id, e);
			return operateError("获取数据失败");
		}
	}
	
	/**
	 * 保存修改后的信息 .
	 * 
	 */
	@RequiresPermissions("drp/drpSupplier/edit")
	@RequestMapping("/edit")
	public ModelAndView edit(@Valid DrpSupplier entity, BindingResult result, DwzAjax dwz) {
		try {
			if(result.hasErrors()){
				return operateError(result.getAllErrors().get(0).getDefaultMessage());
			}
			
			Long id = entity.getId();
			DrpSupplier oldObj = supplierService.selectById(id);
			if (ToolUtil.isEmpty(oldObj)) {
				return operateError("无法获取要修改的数据");
			}
			
			if(!oldObj.getName().equals(entity.getName()) ){
				
				List<DrpSupplier> listByBeanId = supplierService.selectList(new EntityWrapper<DrpSupplier>().eq("name",entity.getName()));
				if (ToolUtil.isNotEmpty(listByBeanId)) {
					return operateError("供货商名称【" + entity.getName() + "】已存在");
				}
				
			}
			
			supplierService.updateById(entity);
			
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("保存修改数据出现异常:", e);
			return operateError("保存失败");
		}
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("drp/drpSupplier/delete")
	@RequestMapping("/delete")
	public ModelAndView delete(Long id, DwzAjax dwz) {
		try {
			DrpSupplier entity = supplierService.selectById(id);
			if (ToolUtil.isEmpty(entity)) {
				return operateError("无法获取要删除的数据");
			}
			
			supplierService.deleteById(id);
			
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("删除出现异常:id="+id, e);
			return operateError("删除失败");
		}
	}
	
}
